package com.dmaximaging.fancypants {
	import flash.geom.Rectangle;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	
	import com.dmaximaging.fancypants.FancyItems;
	
	
	// Not using yet, but will soon:
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class FancyPants extends Sprite {
		const FANCYPANTS_URL:String = "fancypants.xml";
		
		private var fullsize_bool:Boolean;
		public var fancyitems:FancyItems;
		private var fancycontrol:FancyControl;
		
		private var rect:Rectangle;
		
		// XML Stuff
		private var fancypants_loader:URLLoader;
		private var fancypants_xml:XML;
		private var fancyitems_url:String;
		private var fancyitems_loader:URLLoader;
		private var fancyitems_xml:XML;
		
		// Remove after debug
		private var canvas_mc:MovieClip;
				
		public function FancyPants( canvas_mc:MovieClip, rect:Rectangle = null ){
			this.canvas_mc = canvas_mc;		// Remove after debug
			this.rect = rect;
			
			// If a rect was passed, this FancyPants will not actively scale/resize
			fullsize_bool = rect == null ? true : false;
			
			// Add ourselves to the canvas
			//canvas_mc.addChild( this );
			
			initData();
		}
		
		private function initDisplay( rect:Rectangle ):void {
			canvas_mc.addChild( this );
				
			if( fullsize_bool ){		// Setup active resizing
				// Set stage to not scale, and stay in the upper-left
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
			
				// Add an event handler for resizing
				stage.addEventListener( Event.RESIZE, resizeHandler );
				resizeHandler( null );
			}else{						// Setup the static sprite
				//x = rect.x;
				//y = rect.y;
				//width = rect.width;
				//height = rect.height;
			}			
		}
		
		private function initData():void {
			// Load fancypants.xml primary config file
			fancypants_loader = new URLLoader();
			fancypants_loader.addEventListener( Event.COMPLETE, fancypantsLoaded );
			fancypants_loader.load( new URLRequest( FANCYPANTS_URL ) );
		}
		
		private function fancypantsLoaded( event:Event ){
			// Build the XMl object
			fancypants_xml = new XML( fancypants_loader.data );
			
			// Eventual extra config from fancypants.xml would go here
			
			// Load the 
			fancyitems_loader = new URLLoader();
			fancyitems_loader.addEventListener( Event.COMPLETE, fancyitemsLoaded );
			fancyitems_loader.load( new URLRequest( fancypants_xml.fancyitems ) );
		}
		
		private function fancyitemsLoaded( event:Event ){
			fancyitems_xml = new XML( fancyitems_loader.data );
			
			//trace( fancyitems_xml );
			fancyitems = new FancyItems( fancyitems_xml.item, itemsLoaded );
		}
		
		private function itemsLoaded():void {
			trace("dude!");
			fancyitems.render();
			//addChild( fancyitems );
			//fancyitems.x = fancyitems.y = 0;
			//canvas_mc.addChild( fancyitems.items[0] );
			//canvas_mc.addChild( fancyitems );
			addChild( fancyitems );
			//canvas_mc.addChild( this );
			
			initDisplay( rect );
			initControl();
		}
		
		private function initControl():void {
			fancycontrol = new FancyControl( this );
		}
		
		private function resizeHandler( event:Event ){
			//trace("resizing");
			//x = y = 0;
			//width = stage.stageWidth;
			//height = stage.stageHeight;
		}
	}
}